<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Queue;

class Manage extends Common
{
    /**
     * 作者信息
     * */
    public function setAuthor()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $img = $input['img'];
            $name = $input['name'];
            $fens_count = $input['fens_count'];

            if (empty($img)) adminReturn(2, '请上传头像');
            if (empty($name)) adminReturn(2, '请输入昵称');

            $data = [
                'img' => $img,
                'name' => $name,
                'fens_count' => $fens_count
            ];

            Db::name('author')->where(['id' => 1])->update($data);

            adminReturn(1, '保存成功');
        } else {
            $info = Db::name('author')->where(['id' => 1])->find();

            $this->assign('info', $info);
            $this->assign('title', '编辑作者信息');

            return $this->view->fetch();
        }
    }

    /**
     * 套餐列表
     * */
    public function mealList()
    {
        $input = Request()->param();

        $status = $input['status'];

        $where = [];
        if ($status) $where['status'] = $status;

        $list = Db::name('meal')->where($where)->order('sort asc, id desc')->paginate();

        $this->assign('list', $list);
        $this->assign('title', '套餐列表');
        return $this->view->fetch();
    }

    /**
     * 套餐排序
     * */
    public function sortMeal()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $sortObj = json_decode($input['sortObj'], true);
            $res = model('meal')->saveAll($sortObj);
            if ($res) {
                adminReturn(1, '更新排序成功');
            } else {
                adminReturn(2, '更新排序失败');
            }
        }
    }

    /**
     * 新增/编辑 套餐
     * */
    public function doMeal()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $id = $input['id'];
            $img = $input['img'];
            $name = $input['name'];
            $desc = $input['desc'];
            $icon = $input['icon'];
            $tags = $input['tags'];
            $status = $input['status'];

            if (empty($img)) adminReturn(2, '请上传封面图');
            if (empty($name)) adminReturn(2, '请输入名称');
            if (empty($desc)) adminReturn(2, '请输入描述');

            $where['name'] = $name;
            if ($id) $where['id'] = ['neq', $id];
            $count = Db::name('meal')->where($where)->count();
            if ($count > 0) adminReturn(2, '该名称已存在');

            $data = [
                'img' => $img,
                'name' => $name,
                'desc' => $desc,
                'icon' => $icon,
                'tags' => $tags,
                'status' => $status,
            ];

            if ($id > 0) {
                $res = Db::name('meal')->where(['id' => $id])->update($data);
                if ($res) {
                    adminReturn(1, '编辑套餐成功', url('mealList'));
                } else {
                    adminReturn(2, '编辑失败或未编辑');
                }
            } else {
                $res = Db::name('meal')->insert($data);
                if ($res) {
                    adminReturn(1, '新增套餐成功', url('mealList'));
                } else {
                    adminReturn(2, '新增套餐失败');
                }
            }
        } else {
            $id = Request()->param('id');

            if ($id) $info = Db::name('meal')->where(['id' => $id])->find();

            $this->assign('info', $info);
            $this->assign('title', ($id ? '编辑' : '新增') . '套餐');

            return $this->view->fetch();
        }
    }

    /**
     * 删除套餐
     * */
    public function delMeal()
    {
        $id = Request()->param('id');

        if (empty($id)) adminReturn(2, '套餐ID缺失');

        $info = Db::name('meal')->where(['id' => $id])->find();
        if ($info) {
            $res = Db::name('meal')->where(['id' => $id])->delete();
            if ($res) {
                adminReturn(1, '删除成功');
            } else {
                adminReturn(2, '删除失败');
            }
        } else {
            adminReturn(2, '数据不存在或已删除');
        }
    }

    /**
     * 视频列表
     * */
    public function videoList()
    {
        $input = Request()->param();

        $meal_id = $input['meal_id'];
        $status = $input['status'];

        $where = [];
        if ($meal_id) $where['v.meal_id'] = $meal_id;
        if ($status) $where['v.status'] = $status;

        $list = Db::name('meal_video v')->join('vi_meal m', 'v.meal_id = m.id')
            ->field('v.*, m.name as meal_name')
            ->where($where)
            ->order('v.sort asc, v.id desc')
            ->paginate();

        $this->assign('list', $list);
        $this->assign('title', '视频列表');

        $meal_list = Db::name('meal')->where(['status' => 1])->order('id desc')->select();
        $this->assign('meal_list', $meal_list);

        return $this->view->fetch();
    }


    /**
     * 套餐排序
     * */
    public function sortVideo()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $sortObj = json_decode($input['sortObj'], true);
            $res = model('meal_video')->saveAll($sortObj);
            if ($res) {
                adminReturn(1, '更新排序成功');
            } else {
                adminReturn(2, '更新排序失败');
            }
        }
    }

    /**
     * 新增/编辑 视频
     * */
    public function doVideo()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $id = $input['id'];
            $meal_id = $input['meal_id'];
            $path = $input['path'];
            $file_id = $input['file_id'];
            $name = $input['name'];
            $zan_count = $input['zan_count'] ? : 0;
            $status = $input['status'];

            if (empty($meal_id)) adminReturn(2, '请选择套餐');
            if (empty($path)) adminReturn(2, '请上传视频');

            $data = [
                'meal_id' => $meal_id,
                'name' => $name,
                'path' => $path,
                'zan_count' => $zan_count,
                'status' => $status,
            ];

            if ($id > 0) {
                // 查询原有视频路径，判断是否重新上传了视频
                $originalInfo = Db::name('meal_video')->where(['id' => $id])->find();

                // 如果路径不同或提供了新的file_id，表示重新上传了视频
                if (($originalInfo && $originalInfo['path'] != $path) || !empty($file_id)) {
                    // 使用file_id直接查询vi_file表，检查视频是否已经转换完成
                    $fileInfo = Db::name('file')
                        ->where(['id' => $file_id])
                        ->find();

                    // 根据文件状态设置convert_status
                    $data['convert_status'] = $fileInfo['status'];
                }
                
                $res = Db::name('meal_video')->where(['id' => $id])->update($data);
                if ($res) {
                    adminReturn(1, '编辑成功', url('videoList'));
                } else {
                    adminReturn(2, '编辑失败或未编辑');
                }
            } else {
                // 使用file_id直接查询vi_file表，检查视频是否已经转换完成
                $fileInfo = Db::name('file')
                    ->where(['id' => $file_id])
                    ->find();

                // 根据文件状态设置convert_status
                $data['convert_status'] = $fileInfo['status'];

                $res = Db::name('meal_video')->insert($data);
                if ($res) {
                    adminReturn(1, '新增成功', url('videoList'));
                } else {
                    adminReturn(2, '新增失败');
                }
            }
        } else {
            $id = Request()->param('id');

            if ($id) $info = Db::name('meal_video')->where(['id' => $id])->find();

            $this->assign('info', $info);
            $this->assign('title', ($id ? '编辑' : '新增') . '视频');

            $meal_list = Db::name('meal')->where(['status' => 1])->order('id desc')->select();
            $this->assign('meal_list', $meal_list);

            return $this->view->fetch();
        }
    }

    /**
     * 删除视频
     * */
    public function delVideo()
    {
        $id = Request()->param('id');

        if (empty($id)) adminReturn(2, '视频ID缺失');

        $info = Db::name('meal_video')->where(['id' => $id])->find();
        if ($info) {
            $res = Db::name('meal_video')->where(['id' => $id])->delete();
            if ($res) {
                adminReturn(1, '删除成功');
            } else {
                adminReturn(2, '删除失败');
            }
        } else {
            adminReturn(2, '数据不存在或已删除');
        }
    }
}
